package presentation.helpForm_UI;

import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.AbstractListModel;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import presentation.menuForm_UI.Picture;

@SuppressWarnings({ "serial", "unchecked", "rawtypes" })
public class HelpForm_UI extends JDialog {

        private static final long serialVersionUID = 1L;
        
        private JList listHelp = new JList();
        
        private JScrollPane scrollList = new JScrollPane();
        private JScrollPane scrollIntro = new JScrollPane();
        
        private Picture pic = new Picture();

        public HelpForm_UI(JFrame frame) {
                super(frame, true);

                initComponents();

                this.setSize(600, 520);
                this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                this.setLocationRelativeTo(frame);
                this.setResizable(false);
                this.setTitle("Help");
        }

        private void initComponents() {
        	setMainLayout();
        	setListProperties();
        	setPicProperties();
        	listEvent();
        	keyEvent();
        }
        
        private void setMainLayout()
        {
        	this.setLayout(null);
        	this.add(scrollList);
        	this.add(scrollIntro);
        	
        	scrollList.setBounds(5, 5, 140, 482);
        	scrollIntro.setBounds(scrollList.getX() + scrollList.getWidth()+5, scrollList.getY(), 440, 482);
        }
        
		private void setListProperties()
        {
        	scrollList.setViewportView(listHelp);
        	listHelp.setModel(new AbstractListModel() 
        	{
        		String[] item = {"Game Introduction", "Help Topics"};
        		public int getSize()
        		{
        			return item.length;
        		}
        		
        		public Object getElementAt(int i)
        		{
        			return item[i];
        		}
			});
        	listHelp.setSelectedIndex(0);
        }
		
		private void setPicProperties()
		{
			scrollIntro.setViewportView(pic);
			pic.path ="src/image/tooltips/About game.png";
			pic.repaint();
		}
        
        private void listEvent()
        {
        	listHelp.addListSelectionListener(new ListSelectionListener() {
				
				@Override
				public void valueChanged(ListSelectionEvent e) {
					if(listHelp.getSelectedIndex() == 0)
					{
						pic.path = "src/image/tooltips/About game.png";
						pic.repaint();
						pic.setPreferredSize(new Dimension(410, 480));
						scrollIntro.setViewportView(pic);
					}else if(listHelp.getSelectedIndex() == 1)
					{
						pic.path = "src/image/tooltips/How to play.png";
						pic.setPreferredSize(new Dimension(410,880));
						scrollIntro.setViewportView(pic);
						
						pic.repaint();
						
					}
				}
			});
        }
        
        private void keyEvent()
        {
        	listHelp.addKeyListener(new KeyAdapter()
        	{
        		@Override
        		public void keyPressed(KeyEvent evt)
        		{
        			if(evt.getKeyCode() == KeyEvent.VK_ESCAPE)
        			{
        				dispose();
        			}
        		}
        	});
        }
}
